package org.overtheair.say.domain;

import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;
import org.openintents.intents.WikitudePOI;
import org.overtheair.say.common.GlobalBag;
import org.overtheair.say.logic.DataProviderFlickr;

import android.util.Log;


public class DataFlickrPhoto
	extends DataAncestor
{

	// ************************************************************************
	// ctor
	// ************************************************************************
	public DataFlickrPhoto()
	{
	}
	
	public DataFlickrPhoto(String jsonStrem)
	{
		buildFromJSON(jsonStrem);
	}
	
	// ************************************************************************
	// properties
	// ************************************************************************
	public long id;
	public static final String PROP_ID = "id";
	
	public String title;
	public static final String PROP_TITLE = "title";

	public String description;
	public static final String PROP_DESCRIPTION = "description";
	
	public String uri;
	public static final String PROP_URI = "uri";
	
	public Date date_uploaded;
	public static final String PROP_DATE_UPLOADED = "uploaded";
	public Date date_taken;
	public static final String PROP_DATE_TAKEN = "taken";
	
	public String owner_username;
	public static final String PROP_OWNER_USERNAME = "username";
	public String owner_realname;
	public static final String PROP_OWNER_REALNAME = "realname";
	
	public double location_latitude;
	public static final String PROP_LOCATION_LATITUDE = "latitude";
	public double location_longitude;
	public static final String PROP_LOCATION_LONGITUDE = "longitude";
	public String location_woeid;
	public static final String PROP_LOCATION_WOEID = "woeid";
	
	public String locality_name;
	public static final String PROP_LOCALITY_NAME = "content";

	protected static final String PROP_NESTEDOBJ_LOCATION = "location";
	protected static final String PROP_NESTEDOBJ_OWNER = "owner";
	protected static final String PROP_NESTEDOBJ_LOCALITY = "locality";




	// ************************************************************************
	// interface implementation
	// ************************************************************************
	@Override
	public void buildFromJSON(String jsonStream) {
		JSONObject obj;
		JSONObject objNested;
		JSONObject objNested2;
		
		
		try {
			obj = new JSONObject(jsonStream);
			
			this.id = obj.optLong(PROP_ID, GlobalBag.INVALID_DATA_KEY_LONG);
			this.title = obj.optString(PROP_TITLE, "");
			this.description = obj.optString(PROP_DESCRIPTION, "");

			objNested = obj.optJSONObject(PROP_NESTEDOBJ_LOCATION);
			if (null == objNested)
			{
				id = GlobalBag.INVALID_DATA_KEY_LONG;
				return;
			}
			location_latitude = objNested.optDouble(PROP_LOCATION_LATITUDE, 0);
			location_longitude = objNested.optDouble(PROP_LOCATION_LONGITUDE, 0);
			location_woeid = objNested.optString(PROP_LOCATION_WOEID, "");
			objNested2 = objNested.optJSONObject(PROP_NESTEDOBJ_LOCALITY);
			if (null == objNested2)
			{
				id = GlobalBag.INVALID_DATA_KEY_LONG;
				return;
			}
			locality_name = objNested.optString(PROP_LOCALITY_NAME, "");
			
			objNested2 = obj.optJSONObject("urls");
				objNested2 = objNested2.optJSONObject("url");
				uri = objNested2.optString("content");
			
			objNested = obj.optJSONObject(PROP_NESTEDOBJ_OWNER);
			if (null == objNested)
			{
				id = GlobalBag.INVALID_DATA_KEY_LONG;
				return;
			}
			owner_realname = objNested.optString(PROP_OWNER_REALNAME, "");
			owner_username = objNested.optString(PROP_OWNER_USERNAME, "");

		} catch (JSONException e) {
			this.id = GlobalBag.INVALID_DATA_KEY_LONG;
		}
	}

	@Override
	public boolean isDataValid() {
		return id == GlobalBag.INVALID_DATA_KEY_LONG;
	}

	@Override
	public WikitudePOI toWikitudePOI()
	{
		WikitudePOI poi;
		String convuri;
		
		convuri = DataProviderFlickr.getFlickrThumbUrl(uri);
		Log.d("FINDSER", convuri);
		
		poi = new WikitudePOI(
				location_latitude,
				location_longitude,
				0,
				title,
				description + ", " + locality_name,
				uri,
				null,
				null,
				convuri
				);
		return poi;
	}

}
